package com.red.star.macalline.sched.task;

import com.red.star.macalline.sched.service.LogService;
import com.red.star.macalline.sched.service.StoredCardService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author AMGuo
 * @Description
 * @date 2018/03/14 15:18
 */
@Configuration
@EnableScheduling
public class CommonTask {

    private static final Logger LOGGER = LoggerFactory.getLogger(CommonTask.class);

    public static Integer days;

    public static String dayPage;

    @Resource
    LogService logService;

    @Resource
    StoredCardService storedCardService;

    //每三天10点运行
//    @Scheduled(cron = "0 0 10 */3 * *")
    public void masssendMessage() {
        // 查询day之内活动的用户
        List<String> accessLogList = logService.findAccessLogByDay(days);
        // 查询需要显示的模板内容
        List<Map> map = new ArrayList<>();
        try {
            map = storedCardService.getTemplateMsg(accessLogList, dayPage);
        } catch (IOException e) {
            LOGGER.error(e.getMessage());
        }
        // 群发模板消息
        try {
            storedCardService.sendTemplateMsg(map);
        } catch (IOException e) {
            LOGGER.error(e.getMessage());
        }
    }
}
